package service

import (
	"Apply1/ccCustomer/model"
	_ "fmt"
)

//声明一个结构体 完成对customer的操作
//包括增删改查
type CustomerService struct {
	//定义一个customer切片
	customer []model.Customer
	//定义一个字段，显示切片含有多少客户，还可以在此基础上增加新客户
	customerNum int
}

//定义一个方法，可以返回结构体
func NewCustomerService() *CustomerService {
	//为了开始能看见客户，初始化一个
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customer := model.NewCustomer(1, "cc", "男", 18, 123456, "123@163.com")
	customerService.customer = append(customerService.customer, customer)
	return customerService
}

//返回客户列表
func (this *CustomerService) List() []model.Customer {
	return this.customer
}

//添加客户到customer切片中
func (this *CustomerService) Add(customer model.Customer) bool {
	//确定一个分配id规则
	this.customerNum++
	customer.Id = this.customerNum
	this.customer = append(this.customer, customer)
	return true
}

//根据id删除客户
func (this *CustomerService) Delete(id int) bool {
	index := this.FindById(id)
	if index == -1 {
		return false
	}
	//删除切片中一个元素
	this.customer = append(this.customer[:index], this.customer[index+1:]...)
	return true
}

//根据id查找客户对应的下标
func (this *CustomerService) FindById(id int) int {
	index := -1
	//遍历切片
	for i := 0; i < len(this.customer); i++ {
		if this.customer[i].Id == id {
			//找到
			index = i
		}
	}
	return index
}

//修改客户Update方法
